/*
 * @Author: xiaosihan 
 * @Date: 2024-07-23 21:44:51 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-09-17 17:01:54
 */

import partsApi from "@api/partsApi";
import { PartData } from "@type";
import { find, get } from "lodash";
import { configure, observable } from "mobx";
configure({ enforceActions: "never" });

const projectPartsLibraryStore = observable({

    // 类目
    category: '',
    setCategory(category: string) {
        this.category = category;
        this.getPartList(this.projectid);
    },

    projectid: "" as string,
    page: 1,
    pageSize: 10,
    totalCount: 0,
    loading: false,
    //零件数据列表
    partList: [] as Array<PartData>,
    setPage(page: number) {
        this.page = page;
        this.getPartList(this.projectid);
    },

    //获取零件列表
    async getPartList(projectId: string) {
        this.loading = true;
        const { page, pageSize, category } = this;
        const modalTypeResult = await partsApi.getModelType();
        const modalTypeList = get(modalTypeResult, ["data", "data"]);
        let data: any = { projectId };
        if (category === "collaborate") {
            Object.assign(data, { "shared": true });
        } else if (category === "2d") {
            const typeId = get(find(modalTypeList, d => d.name === "工程图纸"), ["id"]);
            Object.assign(data, { "typeId": typeId, "shared": false });
        } else if (category === "3d") {
            const typeId = get(find(modalTypeList, d => d.name === "3D模型"), ["id"]);
            Object.assign(data, { "typeId": typeId, "shared": false });
        }
        const result = await partsApi.get_list(page, pageSize, data);
        const list = get(result, ["data", "data"]);
        const totalCount = get(result, ["data", "totalCount"]);
        this.partList = list;
        this.totalCount = Number(totalCount);
        this.loading = false;
    }

}, {}, { deep: false });

export default projectPartsLibraryStore;